﻿using System;
using System.Collections.Generic;
using SortOf;

namespace Example5
{
    class Program
    {
        enum DigitType { ones = 1, tens = 10 }

        static void Main(string[] args)
        {


            Queue studentQueue = new Queue();


            //Start the queue test with a enqueue and a dequeue
            Console.WriteLine("Fill a queue with students");
            Student st1 = new Student("Justin", 1);
            Student st2 = new Student("Geert", 2);
            Student st3 = new Student("Roy", 3);
            Student st4 = new Student("Geoffrey", 4);
            Student st5 = new Student("Tedjan", 5);
            Student st6 = new Student("Mark", 6);
            Student st7 = new Student("Bernard", 7);
            Student st8 = new Student("Tim", 8);

            studentQueue.EnQueue(st1);
            studentQueue.EnQueue(st2);
            studentQueue.EnQueue(st3);
            studentQueue.EnQueue(st4);
            studentQueue.EnQueue(st5);
            studentQueue.EnQueue(st6);
            studentQueue.EnQueue(st7);
            studentQueue.EnQueue(st8);

            Console.WriteLine("");
            for (int i = 0; i < studentQueue.Count(); i++)
            {
                Console.WriteLine(studentQueue.ToArray()[i]);
            }

            Console.WriteLine("");
            Console.WriteLine("Delete 2 items");
            Console.WriteLine("");

            studentQueue.DeQueue();
            studentQueue.DeQueue();

            for (int i = 0; i < studentQueue.Count(); i++)
            {
                Console.WriteLine(studentQueue.ToArray()[i]);
            }

            Console.WriteLine("");
            Console.WriteLine("Adding 1 item");
            Console.WriteLine("");

            studentQueue.EnQueue(st1);

            for (int i = 0; i < studentQueue.Count(); i++)
            {
                Console.WriteLine(studentQueue.ToArray()[i]);
            }
            //End of the enqueue dequeue test run

            Console.ReadKey();
            Console.Clear();


            //Start numeric sorting with a queue
            Console.WriteLine("Now sorting the a numeric data with queue's");

            int[] nums = new int[] { 19, 46, 85, 15, 92, 35, 31, 22 };

            Console.WriteLine("Number array now.");
            for (int i = 0; i < nums.Length; i++)
            {
                Console.WriteLine(nums[i] + " ");
            }
            
            nums = BuildArray(RSort(nums, DigitType.ones));

            Console.WriteLine("Number array after one run");
            for (int i = 0; i < nums.Length; i++)
            {
                Console.WriteLine(nums[i] + " ");
            }

            nums = BuildArray(RSort(nums, DigitType.tens));

            Console.WriteLine("Number array after second run");
            for (int i = 0; i < nums.Length; i++)
            {
                Console.WriteLine(nums[i] + " ");
            }
            //Sorting done

            Console.ReadKey();
            Console.Clear();


            //Start priority dequeing 
            Console.WriteLine("And now the priority queue sorting 5 = highest 0 = lowest");

            Console.WriteLine(st1 + " with priority 2");
            Console.WriteLine(st2 + " with priority 4");
            Console.WriteLine(st3 + " with priority 1");
            Console.WriteLine(st4 + " with priority 2");

            PriorityQueue pStudents = new PriorityQueue();
            pqItem[] pst = new pqItem[4];

            pqItem removed;
            pst[0].priority = 2;
            pst[0].pObject = st1;
            pst[1].priority = 4;
            pst[1].pObject = st2;
            pst[2].priority = 1;
            pst[2].pObject = st3;
            pst[3].priority = 2;
            pst[3].pObject = st4;

            for (int i = 0; i < pst.Length; i++)
            {
                pStudents.EnQueue(pst[i]);
            }

            removed = (pqItem)pStudents.DeQueue();
            Console.WriteLine();
            Console.WriteLine("Removed: " + removed.pObject.ToString());
            Console.ReadKey();
            //End priority dequeing
        }

        //This method sorst the first or the last number in different queue's
        static Queue[] RSort(int[] n, DigitType digit)
        {
            //Creating queue's to start sorting
            Queue[] numQueue = new Queue[10];
            for (int i = 0; i < 10; i++)
            {
                numQueue[i] = new Queue();
            }


            int snum;
            for (int x = 0; x < n.Length; x++)
            {
                if (digit == DigitType.ones)//Define if you want the tens or the ones of the integer.
                {
                    snum = n[x] % 10;
                }
                else
                {
                    snum = n[x] / 10;
                }
                numQueue[snum].EnQueue(n[x]);//Add the number to the right queue
            }

            return numQueue;
        }

        static int[] BuildArray(Queue[] que)// This method converst the generated que to an array with is readable for people
        {
            int size = 0;
            for (int i = 0; i < que.Length; i++)
            {
                size += que[i].Count();
            }

            int[] n = new int[size];

            int y = 0;
            for (int x = 0; x < 10; x++)
            {
                while (que[x].Count() > 0)
                {
                    n[y] = Int32.Parse(que[x].DeQueue().ToString());
                    y++;
                }
            }

            return n;
        }
    }
}
